package com.imy.lua.helper.screen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjectionManager;
import android.os.Handler;
import android.os.Looper;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.event.FloatingEvent;
import com.imy.common.event.TakeScreenEvent;
import com.imy.common.util.ALog;
import com.imy.common.util.FileUtils;
import com.imy.lua.LuaCore;
import com.imy.lua.activity.LuaActivity;
import com.imy.lua.activity.screen.ScreenActivity;
import com.imy.touch.TouchCore;
import com.imy.touch.root.command.ProtocolCreate;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ScreenHelper {
    private static volatile ScreenHelper gCore = new ScreenHelper();
    private static final String TAG = "ScreenHelper";
    private ExecutorService executorService;
    private String screenPath;

    public static ScreenHelper get() {
        return gCore;
    }

    public void clear() {
    }

    private ScreenHelper() {
        executorService = Executors.newSingleThreadExecutor();
    }

    public synchronized void startFastScreen(OnScreenCaptureListener onScreenCaptureListener) {
        String rootScreenFilePath = FileUtils.createRootScreenFilePath();
        String path = rootScreenFilePath + "/" + System.currentTimeMillis() + ".png";
        startScreen(path, onScreenCaptureListener);
    }

    public synchronized void startScreen(String path, OnScreenCaptureListener onScreenCaptureListener) {
        if (TouchCore.get().getClient() != null) {
            rootScreen(path, onScreenCaptureListener);
        } else {
            //使用录屏方式截屏
            ALog.i("非root截屏");
            recordScreen(path, onScreenCaptureListener);
        }
    }

    private OnScreenCaptureListener onScreenCaptureListener;

    private void recordScreen(String path, OnScreenCaptureListener listener) {
        //判断LuaView界面是否存在
        Context context;
        onScreenCaptureListener = listener;
        if (LuaActivity.self == null) {
            context = LuaCore.get().getContext();
        } else {
            context = LuaActivity.self;
        }
        ScreenActivity.start(context, path);
    }


    private void rootScreen(String path, OnScreenCaptureListener onScreenCaptureListener) {
        if (this.screenPath == null || path.equals("")) {
            this.screenPath = path;
            //delete file if exists
            FileUtils.deleteFile(path);
            TouchCore.get().setOnTakScreenListener(new TouchCore.OnTakScreenListener() {
                @Override
                public void TakScreen(TakeScreenEvent event) {
                    if (event.path.equals(path) && event.isSuccess) {
                        if (onScreenCaptureListener != null) {
                            onScreenCaptureListener.onSuccess(event.path);
                        }
                    } else {
                        if (!event.isSuccess) {
                            onScreenCaptureListener.onFail();
                        }
                    }
                    screenPath = null;
                }
            });
            executorService.execute(() -> {
                try {
                    String takScreen = ProtocolCreate.createTakScreen(path);
                    Thread.sleep(600); //必须要延迟一下 猜测是root 使用的无障碍方式截屏 悬浮窗发生变化造成界面绘制 导致截图阻塞
                    TouchCore.get().getClient().getTransceiver().send(takScreen);//截图命令
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }


    public OnScreenCaptureListener getOnScreenCaptureListener() {
        return onScreenCaptureListener;
    }



    public interface OnScreenCaptureListener {
        void onSuccess(String path);

        void onFail();
    }

}
